# --*-- coding: UTF-8 -*-

import sys
sys.path.append('/home/cnn/pro/yazif/vqa/')

import tensorflow as tf
from utils.config import process_config
from utils.dirs import create_dirs
from models.vis_lstm_model import VisLstmModel
from models.vgg_model import VggModel
from data_loader.data_loader import DataLoader
import numpy as np
import os

config = process_config('../configs/lstm.json')

# create the experiments dirs
create_dirs([config.summary_dir, config.checkpoint_dir])

vgg = None

if not config.is_have_img_data:
    vgg = VggModel(config.vgg19_npy_path)
    vgg.build()

model = VisLstmModel(config, mode=1)
model.build_model()

# data = DataLoader(config)
data = DataLoader(config, mode=1)
saver = tf.train.Saver()
save_path = '/home/cnn/pro/yazif/vqa/experiments/vqa/checkpoint/vqa.ckpt-19'

with tf.Session() as sess:

    module_file = save_path
    # module_file = tf.train.latest_checkpoint(save_path)
    print(module_file)
    init = tf.global_variables_initializer()
    sess.run(init)
    saver.restore(sess, module_file)

    total_acc = 0.0

    epoch_num = 1
    batch_size = 200
    for i in range(epoch_num):
        batch_no = 0
        accs = []
        while batch_no * batch_size < data.get_data_size():
            ques, img, answer = data.get_next_batch(batch_no, batch_size)
            if not config.is_have_img_data:
                img = sess.run(vgg.fc7, feed_dict={vgg.rgb : img})
            acc = sess.run([model.accuracy], feed_dict={
                model.img_feat : img,
                model.ques : ques,
                model.answer : answer,
                model.batch_size : batch_size
            })

            batch_no += 1
            print('========>acc:', acc)
            accs.append(acc)

        acc = np.mean(accs)
        print('acc', acc)
